package com.jambo.yudada.scoring;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jambo.yudada.model.dto.question.QuestionContentDTO;
import com.jambo.yudada.model.entity.App;
import com.jambo.yudada.model.entity.Question;
import com.jambo.yudada.model.entity.ScoringResult;
import com.jambo.yudada.model.entity.UserAnswer;
import com.jambo.yudada.model.vo.QuestionVO;
import com.jambo.yudada.service.QuestionService;
import com.jambo.yudada.service.ScoringResultService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
@ScoringStrategyConfig(appType = 1,scoringStrategy = 0)

public class CustomTestScoringStrategy implements ScoringStrategy{
   @Resource
   private QuestionService questionService;
   @Resource
   private ScoringResultService scoringResultService;
    @Override
    public UserAnswer doScore(List<String> choices, App app) throws Exception {
        Question question=questionService.getOne(
                Wrappers.lambdaQuery(Question.class)
                        .eq(Question::getAppId,app.getId()));
        List<ScoringResult>scoringResultList=scoringResultService.list(
                Wrappers.lambdaQuery(ScoringResult.class)
                        .eq(ScoringResult::getAppId,app.getId()));
        Map<String, Integer> optionCount = new HashMap<>();
        QuestionVO questionVO = QuestionVO.objToVo(question);
        List<QuestionContentDTO> questionContent = questionVO.getQuestionContent();
        for (QuestionContentDTO questionContentDTO : questionContent) {
                    for (String answer : choices) {
                        for (QuestionContentDTO.Option option : questionContentDTO.getOptions()) {
                            if (option.getKey().equals(answer)) {
                                String result = option.getResult();

                                if (!optionCount.containsKey(result)) {
                                    optionCount.put(result, 0);
                                }

                                optionCount.put(result, optionCount.get(result) + 1);
                            }
                        }
                    }
                }
                 int maxScore = 0;
        ScoringResult maxScoreResult = scoringResultList.get(0);
                for (ScoringResult scoringResult : scoringResultList) {
                    List<String> resultProp = JSONUtil.toList(scoringResult.getResultProp(), String.class);
                    int score = resultProp.stream()
                            .mapToInt(prop -> optionCount
                                    .getOrDefault(prop, 0))
                            .sum();
                    if (score > maxScore) {
                        maxScore = score;
                        maxScoreResult = scoringResult;
                    }
                }
UserAnswer userAnswer=new UserAnswer();
        userAnswer.setAppId(app.getId());
        userAnswer.setAppType(app.getAppType());
        userAnswer.setScoringStrategy(app.getScoringStrategy());
        userAnswer.setChoices(JSONUtil.toJsonStr(choices));
        userAnswer.setResultId(maxScoreResult.getId());
        userAnswer.setResultName(maxScoreResult.getResultName());
        userAnswer.setResultDesc(maxScoreResult.getResultDesc());
        userAnswer.setResultPicture(maxScoreResult.getResultPicture());
                return userAnswer;
            }
        }
